/*
 * JSelectPersonDialog.java
 *
 * Created on 21 maj 2008, 19:39
 */

package schoolmanagement.dialogs;

import java.util.ArrayList;
import java.util.List;
import schoolmanagement.controller.Role;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import schoolmanagement.controller.ConstantData;
import schoolmanagement.controller.DBAccess;
import schoolmanagement.entity.SmClass;
import schoolmanagement.entity.SmPerson;

/**
 *
 * @author  deely
 */
public class JSelectPersonDialog extends javax.swing.JDialog {
    
    List<SmPerson> m_result;
    /** Creates new form JSelectPersonDialog */
    public JSelectPersonDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        m_result = new ArrayList<SmPerson>();
    }
    
    public List<SmPerson> getResult()
    {
        return m_result;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jRbRole = new javax.swing.JRadioButton();
        jRbClass = new javax.swing.JRadioButton();
        jTbRole = new javax.swing.JComboBox();
        jTbClass = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jRbSurname = new javax.swing.JRadioButton();
        jTbName = new javax.swing.JTextField();
        jBtnSearch = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTable4 = new javax.swing.JTable();
        jbtCancel = new javax.swing.JButton();
        jbtSelect = new javax.swing.JButton();

        setLocationByPlatform(true);
        setName("Form"); // NOI18N
        setResizable(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setName("jPanel2"); // NOI18N

        buttonGroup1.add(jRbRole);
        jRbRole.setText("Grupa:"); // NOI18N
        jRbRole.setName("jRbRole"); // NOI18N

        buttonGroup1.add(jRbClass);
        jRbClass.setText("Klasa:"); // NOI18N
        jRbClass.setName("jRbClass"); // NOI18N

        jTbRole.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dyrekcja", "Nauczyciele", "Uczniowie" }));
        jTbRole.setName("jTbRole"); // NOI18N
        jTbRole.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTbRoleActionPerformed(evt);
            }
        });

        jTbClass.setName("jTbClass"); // NOI18N
        jTbClass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTbClassActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRbRole)
                    .addComponent(jRbClass))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTbRole, 0, 369, Short.MAX_VALUE)
                    .addComponent(jTbClass, 0, 369, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRbRole)
                    .addComponent(jTbRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRbClass)
                    .addComponent(jTbClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        buttonGroup1.add(jRbSurname);
        jRbSurname.setSelected(true);
        jRbSurname.setText("Nazwisko"); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(schoolmanagement.SchoolmanagementApp.class).getContext().getResourceMap(JSelectPersonDialog.class);
        jRbSurname.setActionCommand(resourceMap.getString("jRbSurname.actionCommand")); // NOI18N
        jRbSurname.setName("jRbSurname"); // NOI18N

        jTbName.setName("jTbName"); // NOI18N
        jTbName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTbNameFocusGained(evt);
            }
        });

        jBtnSearch.setText("Szukaj"); // NOI18N
        jBtnSearch.setName("jBtnSearch"); // NOI18N
        jBtnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSearchActionPerformed(evt);
            }
        });

        jScrollPane5.setName("jScrollPane5"); // NOI18N

        jTable4.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nazwisko i Imię", "Grupa"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable4.setName("jTable4"); // NOI18N
        jTable4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable4mouseClicked(evt);
            }
        });
        jScrollPane5.setViewportView(jTable4);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jRbSurname)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTbName, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBtnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRbSurname)
                    .addComponent(jBtnSearch)
                    .addComponent(jTbName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                .addContainerGap())
        );

        jbtCancel.setText("Anuluj"); // NOI18N
        jbtCancel.setName("jbtCancel"); // NOI18N
        jbtCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCancelActionPerformed(evt);
            }
        });

        jbtSelect.setText("Wybierz"); // NOI18N
        jbtSelect.setName("jbtSelect"); // NOI18N
        jbtSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSelectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 168, Short.MAX_VALUE)
                        .addComponent(jbtCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtSelect)
                    .addComponent(jbtCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSearchActionPerformed
        // TODO add your handling code here:
        
        jRbSurname.setSelected(true);
        
        jTable4.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
        },
                new String [] {
            "Nazwisko i Imię", "Grupa"
        }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };
            
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        
        DefaultTableModel model = (DefaultTableModel) jTable4.getModel();
        
        List<SmPerson> list = new ArrayList<SmPerson>();
        if(jRbSurname.isSelected()){
            list = DBAccess.GetInstance().GetUserByName(jTbName.getText().trim());
        } else if(jRbRole.isSelected()){
            list = DBAccess.GetInstance().GetUserByRole( ((Role)jTbRole.getSelectedItem()).GetRoleType());
            
        } else if(jRbClass.isSelected()){
            list = DBAccess.GetInstance().GetPersonsForClass((SmClass)jTbClass.getSelectedItem());
        } else{
            throw new RuntimeException();
        }
        
        for(SmPerson person : list){
            model.addRow(new Object[]{person,
            DBAccess.GetInstance().getUserRole(person).getRolName()});
            //int row = list.indexOf(person);
            //jTable4.setValueAt(person.getPerSurname(), row, 0);
            //jTable4.setValueAt(person.getPerName(), row, 1);
            //jTable4.setValueAt(person.getSmUserCollection().iterator().next().getUsrRolId().getRolName(), row, 2);
        }
    }//GEN-LAST:event_jBtnSearchActionPerformed

    private void jTable4mouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable4mouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            JTable target = (JTable)evt.getSource();
            int row = target.getSelectedRow();
            Object o = target.getValueAt(row, 0);
            if(o == null)
                return;
            SmPerson person = (SmPerson)target.getValueAt(row, 0);
            m_result.add(person);
            setVisible(false);
        }
    }//GEN-LAST:event_jTable4mouseClicked

    private void jbtCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCancelActionPerformed
        m_result = null;
        setVisible(false);
    }//GEN-LAST:event_jbtCancelActionPerformed

    private void jbtSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSelectActionPerformed
        List<SmPerson> list = null;
        if(jRbSurname.isSelected()){
            if( jTable4.getSelectedRowCount() == 0 ) return;
            int[] rows = jTable4.getSelectedRows();
            for( int row : rows )
            {
                Object o = jTable4.getValueAt(row, 0);
                if(o == null) continue;
                SmPerson person = (SmPerson)jTable4.getValueAt(row, 0);
                m_result.add(person);
            }
            setVisible(false);
            return;
        } else if(jRbRole.isSelected()){
            list = DBAccess.GetInstance().GetUserByRole( ((Role)jTbRole.getSelectedItem()).GetRoleType());
        } else if(jRbClass.isSelected()){
            list = DBAccess.GetInstance().GetPersonsForClass((SmClass)jTbClass.getSelectedItem());
        }
        
        m_result = list;
        setVisible(false);
    }//GEN-LAST:event_jbtSelectActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        jTbRole.removeAllItems();
        jTbClass.removeAllItems();
        List<Role> lstRole = ConstantData.GetRoles();
        for( Role role : lstRole)
        {
            jTbRole.addItem(role);
        }
        List<SmClass> lstClass = DBAccess.GetInstance().GetAllClasses();
        for( SmClass smclass : lstClass)
        {
            jTbClass.addItem(smclass);
        }
    }//GEN-LAST:event_formComponentShown

    private void jTbRoleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTbRoleActionPerformed
        jRbRole.setSelected(true);
    }//GEN-LAST:event_jTbRoleActionPerformed

    private void jTbClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTbClassActionPerformed
        jRbClass.setSelected(true);
    }//GEN-LAST:event_jTbClassActionPerformed

    private void jTbNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTbNameFocusGained
        jRbSurname.setSelected(true);
    }//GEN-LAST:event_jTbNameFocusGained
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JSelectPersonDialog dialog = new JSelectPersonDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jBtnSearch;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRbClass;
    private javax.swing.JRadioButton jRbRole;
    private javax.swing.JRadioButton jRbSurname;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTable4;
    private javax.swing.JComboBox jTbClass;
    private javax.swing.JTextField jTbName;
    private javax.swing.JComboBox jTbRole;
    private javax.swing.JButton jbtCancel;
    private javax.swing.JButton jbtSelect;
    // End of variables declaration//GEN-END:variables
    
}
